package com.museum.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.museum.base.BaseResponse;
import com.museum.controller.APi.feignAPI.FeignAPIController;
import com.museum.entity.DO.DepLog;
import com.museum.entity.DO.InputApply;
import com.museum.entity.DTO.InputApplyCol;
import com.museum.mapper.InputApplyColMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Ahm
 * @since 2021-03-15
 */
@Service
public class InputApplyServiceImpl extends ServiceImpl<InputApplyColMapper, InputApplyCol> {
    @Autowired
    private FeignAPIController feignAPIController;
    @Autowired
    private com.museum.mapper.InputApplyColMapper inputApplyColMapper;




    /**
     * @param
     * @param request
     * @Desciption 添加入库申请
     * @return
     */

    public int addInputApply(InputApply inputApply, HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        BaseResponse<JSONObject> userMessageByUSerId = feignAPIController.getUserMessageByUSerId(token);
        JSONObject data = userMessageByUSerId.getData();
        InputApplyCol applyCol = new InputApplyCol();
        Integer id =null;
        String applicant=null;
        String applyDate = null;
        String applyTime =null;
        String applyPlace =null;
        String reason=null;
        String note =null;
        if (inputApply!=null){
            id = inputApply.getId();
            applicant = inputApply.getApplicant();
            applyDate = inputApply.getApplyDate();
            applyTime = inputApply.getApplyTime();
            applyTime = inputApply.getApplyTime();
            applyPlace = inputApply.getApplyPlace();
            reason = inputApply.getReason();
            note = inputApply.getNote();
        }
        applyCol.setId(id);
        applyCol.setAreaNumber(226);
        applyCol.setState(0); // 0 提交 1 审核 -1 审核驳回 2 归档
        applyCol.setApplicant(applicant);
        applyCol.setApplyDate(applyDate);
        applyCol.setApplyTime(applyTime);
        applyCol.setApplyPlace(applyPlace);
        applyCol.setReason(reason);
        applyCol.setNote(note);
        return  inputApplyColMapper.insert(applyCol);
    }

    /**
     * @Description 入库申请查看ById
     *
     */
    public JSONObject getApplyById(Integer id){
        InputApplyCol applyCol = inputApplyColMapper.selectById(id);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("applyCol",applyCol);
        return jsonObject;
    }

    /**
     * @Description 审核通过
     */
    public int updateCheckById(Integer id){
        InputApplyCol applyCol = inputApplyColMapper.selectById(id);
        applyCol.setState(1);
        return inputApplyColMapper.updateById(applyCol);

    }

    /**
     * @Description 审核驳回
     */
    public int disagreeCheckById(Integer id){
        InputApplyCol applyCol = inputApplyColMapper.selectById(id);
        applyCol.setState(-1);
        return inputApplyColMapper.updateById(applyCol);

    }

    /**
     * @Description 归档
     */
    public int fileById(Integer id){
        InputApplyCol applyCol = inputApplyColMapper.selectById(id);
        applyCol.setState(2);
        return inputApplyColMapper.updateById(applyCol);

    }

    /**
     * @param
     * @Desciption 条件查询入库申请
     * @return
     */
    public JSONObject getAllInputApplyByPage(long current, long limit, InputApply inputApply){
        HashMap<String,Object> map =  new HashMap<String,Object>();
        List<InputApplyCol> applyList = new ArrayList<InputApplyCol>();
        Integer id =null;
        String applicant=null;
        String applyDate = null;
        String applyTime =null;
        String applyPlace =null;
        String reason=null;
        String note =null;
        if(inputApply!=null){
          id = inputApply.getId();
          applicant = inputApply.getApplicant();
          applyDate = inputApply.getApplyDate();
          applyTime = inputApply.getApplyTime();
          applyPlace = inputApply.getApplyPlace();
          reason = inputApply.getReason();
          note = inputApply.getNote();
        }
        if (id == null || id == 0) {
            id = null;
        }
        if (!StringUtils.isEmpty(id)) {
            map.put("id",id);
        }
        if (!StringUtils.isEmpty(applicant)) {
            map.put("applicant",applicant);
        }
        if (!StringUtils.isEmpty(applyDate)) {
            map.put("applyDate",applyDate);
        }
        if (!StringUtils.isEmpty(applyTime)) {
            map.put("applyTime",applyTime);
        }
        if (!StringUtils.isEmpty(applyPlace)) {
            map.put("applyPlace",applyPlace);
        }

        if (!StringUtils.isEmpty(reason)) {
            map.put("reason",reason);
        }

        if (!StringUtils.isEmpty(note)) {
            map.put("note",note);
        }

        map.put("current", (current - 1) * limit);
        map.put("limit",limit);
        List<InputApplyCol> list = inputApplyColMapper.selectInputApplyColByPage(map); ;
        for (InputApplyCol app : list) {
            applyList.add(app);
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("total",applyList.size());
        jsonObject.put("applyList", applyList);
        return jsonObject;
    }

    /**
     * @param
     * @Desciption 条件查询入库审核
     * @return
     */
    public JSONObject getAllInputCheckByPage(long current, long limit, InputApply inputApply){
        HashMap<String,Object> map =  new HashMap<String,Object>();
        List<InputApplyCol> applyList = new ArrayList<InputApplyCol>();
        Integer id =null;
        String applicant=null;
        String applyDate = null;
        String applyTime =null;
        String applyPlace =null;
        String reason=null;
        String note =null;
        if(inputApply!=null){
            id = inputApply.getId();
            applicant = inputApply.getApplicant();
            applyDate = inputApply.getApplyDate();
            applyTime = inputApply.getApplyTime();
            applyPlace = inputApply.getApplyPlace();
            reason = inputApply.getReason();
            note = inputApply.getNote();
        }
        if (id == null || id == 0) {
            id = null;
        }
        if (!StringUtils.isEmpty(id)) {
            map.put("id",id);
        }
        if (!StringUtils.isEmpty(applicant)) {
            map.put("applicant",applicant);
        }
        if (!StringUtils.isEmpty(applyDate)) {
            map.put("applyDate",applyDate);
        }
        if (!StringUtils.isEmpty(applyTime)) {
            map.put("applyTime",applyTime);
        }
        if (!StringUtils.isEmpty(applyPlace)) {
            map.put("applyPlace",applyPlace);
        }

        if (!StringUtils.isEmpty(reason)) {
            map.put("reason",reason);
        }

        if (!StringUtils.isEmpty(note)) {
            map.put("note",note);
        }

        map.put("current", (current - 1) * limit);
        map.put("limit",limit);
        List<InputApplyCol> list = inputApplyColMapper.selectApplyCheckByPage(map); ;
        for (InputApplyCol app : list) {
            applyList.add(app);
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("total",applyList.size());
        jsonObject.put("applyList", applyList);
        return jsonObject;
    }

    /**
     * @param
     * @Desciption 条件查询入库处理
     * @return
     */
    public JSONObject getAllInputDealByPage(long current, long limit, InputApply inputApply){
        HashMap<String,Object> map =  new HashMap<String,Object>();
        List<InputApplyCol> applyList = new ArrayList<InputApplyCol>();
        Integer id =null;
        String applicant=null;
        String applyDate = null;
        String applyTime =null;
        String applyPlace =null;
        String reason=null;
        String note =null;
        if(inputApply!=null){
            id = inputApply.getId();
            applicant = inputApply.getApplicant();
            applyDate = inputApply.getApplyDate();
            applyTime = inputApply.getApplyTime();
            applyPlace = inputApply.getApplyPlace();
            reason = inputApply.getReason();
            note = inputApply.getNote();
        }
        if (id == null || id == 0) {
            id = null;
        }
        if (!StringUtils.isEmpty(id)) {
            map.put("id",id);
        }
        if (!StringUtils.isEmpty(applicant)) {
            map.put("applicant",applicant);
        }
        if (!StringUtils.isEmpty(applyDate)) {
            map.put("applyDate",applyDate);
        }
        if (!StringUtils.isEmpty(applyTime)) {
            map.put("applyTime",applyTime);
        }
        if (!StringUtils.isEmpty(applyPlace)) {
            map.put("applyPlace",applyPlace);
        }

        if (!StringUtils.isEmpty(reason)) {
            map.put("reason",reason);
        }

        if (!StringUtils.isEmpty(note)) {
            map.put("note",note);
        }

        map.put("current", (current - 1) * limit);
        map.put("limit",limit);
        List<InputApplyCol> list = inputApplyColMapper.selectInputDealByPage(map); ;
        for (InputApplyCol app : list) {
            applyList.add(app);
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("total",applyList.size());
        jsonObject.put("applyList", applyList);
        return jsonObject;
    }

    /**
     * @param
     * @Desciption 条件查询入库归档
     * @return
     */
    public JSONObject getFileByPage(long current, long limit, InputApply inputApply){
        HashMap<String,Object> map =  new HashMap<String,Object>();
        List<InputApplyCol> applyList = new ArrayList<InputApplyCol>();
        Integer id =null;
        String applicant=null;
        String applyDate = null;
        String applyTime =null;
        String applyPlace =null;
        String reason=null;
        String note =null;
        if(inputApply!=null){
            id = inputApply.getId();
            applicant = inputApply.getApplicant();
            applyDate = inputApply.getApplyDate();
            applyTime = inputApply.getApplyTime();
            applyPlace = inputApply.getApplyPlace();
            reason = inputApply.getReason();
            note = inputApply.getNote();
        }
        if (id == null || id == 0) {
            id = null;
        }
        if (!StringUtils.isEmpty(id)) {
            map.put("id",id);
        }
        if (!StringUtils.isEmpty(applicant)) {
            map.put("applicant",applicant);
        }
        if (!StringUtils.isEmpty(applyDate)) {
            map.put("applyDate",applyDate);
        }
        if (!StringUtils.isEmpty(applyTime)) {
            map.put("applyTime",applyTime);
        }
        if (!StringUtils.isEmpty(applyPlace)) {
            map.put("applyPlace",applyPlace);
        }



        if (!StringUtils.isEmpty(reason)) {
            map.put("reason",reason);
        }

        if (!StringUtils.isEmpty(note)) {
            map.put("note",note);
        }

        map.put("current", (current - 1) * limit);
        map.put("limit",limit);
        List<InputApplyCol> list = inputApplyColMapper.selectFileByPage(map); ;

        for (InputApplyCol app : list) {
            applyList.add(app);
        }
        
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("total",applyList.size());
        jsonObject.put("applyList", applyList);
        return jsonObject;
    }
}
